<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      size="mini"
      class="mt20"
    >
      <el-form-item label="区域名称" prop="name">
        <el-input v-model="ruleForm.name" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="区域类型" prop="areaType">
        <el-select
          v-model="ruleForm.areaType"
          placeholder="请选择区域类型"
          style="width: 200px"
        >
          <el-option
            v-for="item in dict.type.location_zone_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input
          type="textarea"
          v-model="ruleForm.remark"
          style="width: 200px"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确定</el-button
        >
        <!-- <el-button @click="$emit('closed', false)">取消</el-button> -->
        <el-button @click="closeForm()">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  dicts: ["location_zone_type"],

  props: {
    // toEdit: Object,
    // required: true,
    toEdit: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      options: [],
      ruleForm: {
        name: "",
        areaType: "",
        remark: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          {
            min: 2,
            max: 50,
            message: "长度在 2 到 50 个字符",
            trigger: "blur",
          },
        ],
        areaType: [
          { required: true, message: "请选择活动区域", trigger: "change" },
        ],
      },
    };
  },
  watch: {
    toEdit: {
      handler(val, oldVal) {
        this.getEdit();
      },
      deep: true,
    },
  },
  created() {
    // this.getlist();
  },

  mounted() {
    //编辑赋值
    this.getEdit();
  },
  methods: {
    getEdit() {
      if (this.toEdit != {}) {
        this.ruleForm = { ...this.ruleForm, ...this.toEdit };
        this.ruleForm.name = this.toEdit.name;
        this.ruleForm.areaType = this.toEdit.areaType.toString();
      }
    },
    getlist() {
      this.$api.fenceAreaType().then((res) => {
        // console.log(res);
        this.options = res.result;
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit("addgraphInfo", this.ruleForm);
          this.ruleForm = {};
        } else {
          return false;
        }
      });
    },
    closeForm() {
      this.$emit("closed", false);
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
